package com.example.SpringBootDemo.controller;

import com.example.SpringBootDemo.model.Person;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/return")
@Controller
public class RetrunController {
    @RequestMapping("/getIndex")
    public String getIndex() {
        return "/index.html";
    }
    @ResponseBody
    @RequestMapping("/responsetData")
    public String responsetData() {
        return "视图所需要的数据";
    }

    @ResponseBody
    @RequestMapping("/retrunHtml")
    public String retrunHtml() {
        return "<h1> 这是一个代码片段 </1>";
    }


    @ResponseBody
    @RequestMapping("/retrunmHson")
    public Person retrunJson() {
        Person person = new Person();
        person.setName("zhangsan");
        person.setAge(18);
        person.setId(2024);
        return person;
    }
    @ResponseBody
    @RequestMapping("/retrunmMap")
    public Map retrunmMap() {
        Map<String,Integer> map = new HashMap<>();
        map.put("zhang",1);
        map.put("lisit",2);
        map.put("wangwu",3);
        return map;
    }
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(401);// 401通常表示未登录
        return "设置状态码成功";
    }

    @ResponseBody
    // 默认text.html 格式返回
//    @RequestMapping("/r1")
    // 按照json格式返回
    // produces 是设置返回值的类型的,这里设置的是返回的类型是json
    // consumes 是设置请求的类型,比如设置成 json等
    // method 是设置请求的方法 如 post / get
    // params 是必须要有某些值, headers 是 header 必须要有哪些值
    @RequestMapping(value = "/r1",produces = "application/json;charset=utf8")
    public String r1(HttpServletResponse response) {
        // 设置header
        response.setHeader("header","ok");
        return "{'ok':1}";
    }
}
